package com.terra;

import java.text.DecimalFormat;

import com.google.android.maps.GeoPoint;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class WaypointDialog extends Dialog implements android.view.View.OnClickListener {
    
	Button okButton;
    EditText editLat;
    EditText editLon;
    EditText editDistance;
    EditText editBearing;
    EditText editName;
    
	public WaypointDialog(Context context, String strTCID) {
        super(context);
        /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.addwaypoint);
        
        okButton = (Button) findViewById(R.id.buttonOk);
        okButton.setOnClickListener(this);
        
        editLat = (EditText) findViewById(R.id.editTextLat);
        editLat.setText(MakeCoord(Globals.dGPSLat, "NS"));
        editLon = (EditText) findViewById(R.id.editTextLon);
        editLon.setText(MakeCoord(Globals.dGPSLon, "EW"));
        
        editDistance = (EditText) findViewById(R.id.editTextDistance);
        editBearing = (EditText) findViewById(R.id.editTextBearing);
        
        editName = (EditText) findViewById(R.id.editTextName);
        
        editName.setText(strTCID+"-1");
    }

	public String MakeCoord(double val, String strQuad) {

		String strCoord = strQuad.substring(0,1) + " ";
		if( val<0 ) {
			strCoord = strQuad.substring(1,2) + " ";
			val = -val;
		}
		
		int intPart = (int) val;
		strCoord = strCoord + Integer.toString(intPart) + " ";
		
		DecimalFormat threeDec = new DecimalFormat("0.000");
		String shortString = (threeDec.format((val - intPart)*60.));
		strCoord += shortString;
		
		return strCoord;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
        if (v == okButton) {
 
        	Coordinates newCoord = new Coordinates();
        	
        	// set projection to feet if necessary
        	// defaults to meters
        	if( Globals.strUnits.equalsIgnoreCase("English") )
        		newCoord.SetEarthRadius(20902231.);
        	
			newCoord.parse(editLat.getText().toString()+", "+editLon.getText().toString());
			newCoord.Project(Double.parseDouble(editDistance.getText().toString()),
						   Double.parseDouble(editBearing.getText().toString()));
			
			GeoPoint newPoint = new GeoPoint((int) (newCoord.getLatitude()*1e6), (int) (newCoord.getLongitude()*1e6));
			TCOverlayItem newCache = new TCOverlayItem(newPoint, "", "");
			newCache.setDesc("WP");
			newCache.setID("temp");
			newCache.setPosition(newCoord.getLatitude().toString(), newCoord.getLongitude().toString());
			Globals.TempOverlay.addOverlay(newCache, "");
			
			Globals.mapView.getOverlays().clear(); 
			if (Globals.TCOverlay!=null )
				Globals.mapView.getOverlays().add(Globals.TCOverlay);
			if( Globals.TempOverlay!=null )
				Globals.mapView.getOverlays().add(Globals.TempOverlay);
			Globals.mapView.getOverlays().add(Globals.myLocationOverlay);
			
            dismiss();
        }
	}

}
